package com.java110.mall.shop.cmd.shop;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountDto;
import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.store.StoreDto;
import com.java110.intf.acct.IAccountBondObjInnerServiceSMO;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.goods.IDistributionModeInnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.po.account.AccountPo;
import com.java110.po.distributionMode.DistributionModePo;
import com.java110.po.shop.ShopPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 添加商户信息
 */

@Java110Cmd(serviceCode = "shop.adminAddShop")
public class AdminAddShopCmd extends Cmd {

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;

    @Autowired
    private IAccountBondObjInnerServiceSMO accountBondObjInnerServiceSMOImpl;

    @Autowired
    private IDistributionModeInnerServiceSMO distributionModeInnerServiceSMOImpl;

    @Autowired
    private IStoreV1InnerServiceSMO storeV1InnerServiceSMOImpl;


    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "address", "未包含店铺地址");
        Assert.hasKeyAndValue(reqJson, "areaAddress", "未包含区域地址");
        Assert.hasKeyAndValue(reqJson, "areaCode", "未包含区域");
        Assert.hasKeyAndValue(reqJson, "storeId", "未包含商户公司");
        Assert.hasKeyAndValue(reqJson, "shopName", "未包含店铺名称");
        Assert.hasKeyAndValue(reqJson, "shopType", "未包含店铺类型");
        Assert.hasKeyAndValue(reqJson, "tel", "未包含联系电话");
    }

    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(reqJson.getString("storeId"));
        List<StoreDto> storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);

        Assert.listOnlyOne(storeDtos, "商户不存在");
        //保存商铺
        saveShop(storeDtos.get(0), reqJson);
    }

    private void saveShop(StoreDto storePo, JSONObject reqJson) {
        ShopPo shopPo = new ShopPo();
        shopPo.setShopName(reqJson.getString("shopName"));
        shopPo.setShopDesc("店铺比较懒未完善店铺信息");
        shopPo.setReturnPerson(reqJson.getString("shopName"));
        shopPo.setReturnLink(reqJson.getString("tel"));
        shopPo.setStoreId(storePo.getStoreId());
        shopPo.setSendAddress(reqJson.getString("areaAddress") + reqJson.getString("address"));
        shopPo.setReturnAddress(reqJson.getString("areaAddress") + reqJson.getString("address"));
        shopPo.setShopType(reqJson.getString("shopType"));
        shopPo.setOpenType(ShopDto.OPEN_TYPE_SHOP);
        shopPo.setShopRange("无");
        shopPo.setAreaCode(reqJson.getString("areaCode"));
        shopPo.setShopId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_shopId));
        shopPo.setShopLogo("");
        shopPo.setMapX("1");
        shopPo.setMapY("1");

        shopPo.setState(ShopDto.STATE_Y);
        //shopPo.setState(ShopDto.STATE_B);

        int flag = shopInnerServiceSMOImpl.saveShop(shopPo);
        if (flag < 1) {
            throw new CmdException("保存商铺失败");
        }

        //默认保存保证金应收信息
        //saveAccountBond(accoutBond, shopPo);

        //添加派送方式
        DistributionModePo distributionModePo = new DistributionModePo();
        distributionModePo.setFixedFee("0");
        distributionModePo.setbId("-1");
        distributionModePo.setMaximumDistribution("-1");
        //处理商品与配送方式关系
        distributionModePo.setModeId(GenerateCodeFactory.getGeneratorId("16"));
        distributionModePo.setShopId(shopPo.getShopId());
        distributionModePo.setModeName("到店自提");
        distributionModePo.setModeType(DistributionModeDto.MODE_TYPE_USER);
        distributionModePo.setStartDeliveryIn("-1");
        distributionModePo.setStartDeliveryOut("-1");
        flag = distributionModeInnerServiceSMOImpl.saveDistributionMode(distributionModePo);
        if (flag < 1) {
            throw new CmdException("保存店铺配送方式失败");
        }

        reqJson.put("shopId", shopPo.getShopId());

    }


}
